package com.koi.clubservice.controller;


import com.koi.clubservice.domain.po.Club;
import com.koi.clubservice.domain.po.Users;
import com.koi.clubservice.service.IClubService;
import com.koi.mccommon.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 社团信息表 前端控制器
 * </p>
 *
 * @author koi
 * @since 2025-03-03
 */
@RestController
@Slf4j
@RequestMapping("/myclub/club")
@Api(tags = "社团信息")
public class ClubController {

    @Autowired
    private IClubService clubService;

    @Autowired
    private MongoTemplate mongoTemplate;

    //获取社团
    @PostMapping  ("/getClub/{clubId}")
    @ApiOperation(value = "获取社团")
    public Result<Club> getClub(@PathVariable Integer clubId){
        Club club =clubService.getById(clubId);
        log.info("查询社团信息"+club);
        return Result.success(club);
    }

    //mongodb新增uesr
    @PostMapping("/addUsers")
    @ApiOperation(value = "新增用户")
    public Result addUsers(@RequestBody Users users){
        Users save = mongoTemplate.save(users);
        return Result.success(save);
    }

    //mongodb查询uesr
    @PostMapping("/getUsers")
    @ApiOperation(value = "查询用户")
    public Result getUsers(@RequestBody Users users){
        Query  query = new Query();
        Criteria criteria = Criteria.where("name").is(users.getName());
        query.addCriteria(criteria);
        Users users1 = mongoTemplate.findOne(query, Users.class);
        //Update update = new Update().set("name",users.getName());
        //mongoTemplate.updateFirst(query,update,Users.class);

        return Result.success(users1);
    }

    @PostMapping("/updateUsers")
    @ApiOperation(value = "更新用户")
    public Result updateUsers(@RequestBody Users users){
        Query  query = new Query();
        Criteria criteria = Criteria.where("name").is(users.getName());
        query.addCriteria(criteria);
        Update update = new Update().set("sex",users.getSex());
        mongoTemplate.updateFirst(query,update,Users.class);
        return Result.success();
    }

    @PostMapping("/deleteUsers")
    @ApiOperation(value = "删除用户")
    public Result deleteUsers(@RequestBody Users users){
        Query  query = new Query();
        Criteria criteria = Criteria.where("name").is(users.getName());
        query.addCriteria(criteria);
        mongoTemplate.remove(query,Users.class);
        return Result.success();
    }


}
